package com.aos.erp.message.rpc;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.message.dto.MessageTemplateDTO;
import com.aos.erp.message.dto.MessageTemplateItemDTO;
import com.aos.erp.message.model.MessageTemplateInfo;
import com.aos.erp.message.model.vo.MTConditionVo;
import com.aos.erp.message.model.vo.MessageTemplateVo;
import com.aos.erp.message.service.MessageTemplateService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = {"服务接口：消息模板接口"})
@RestController
@RequestMapping("/service/message/template")
public class RpcMessageTemplateController {

    @Autowired
    MessageTemplateService messageTemplateService;

    @ApiOperation(value = "保存消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mtTitle", value = "模板标题(100字符内)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pushTypeList", value = "推送方式数组[{mtContent:'消息模板(1000字符内，APP、短信推送需要)',mtWxTemplateId:'微信模板ID,微信公众号、微信小程序推送需要',mtUrl:'跳转链接,微信公众号、微信小程序推送需要，其中小程序仅限小程序内的页面;可不传,点击后无跳转',pushType:推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；sms:短信),pushParam:[{mtpId:参数项ID,mtpItem:'参数项', mtpDesc:'参数项描述'}] }]", required = true, dataType = "List"),
            @ApiImplicitParam(name = "mtDriverSchoolList", value = "模板下发驾校数组[{dsId:0,dsCode:'驾校编号',dsName:'驾校名称'}]", required = true, dataType = "List"),
            @ApiImplicitParam(name = "optUser", value = "操作人", required = true, dataType = "Long", paramType = "Query")
    })
    @PostMapping("/add")
    public ResultEntity addTemplate(@ApiParam("入参") @RequestBody MessageTemplateVo params) throws Exception{
        messageTemplateService.saveTemplate(params);
        return ResultEntity.success("新增成功");
    }

    @ApiOperation(value = "编辑消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模板ID", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "mtTitle", value = "模板标题(100字符内)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pushTypeList", value = "推送方式数组[{mtContent:'消息模板(1000字符内，APP、短信推送需要)',mtWxTemplateId:'微信模板ID,微信公众号、微信小程序推送需要',mtUrl:'跳转链接,微信公众号、微信小程序推送需要，其中小程序仅限小程序内的页面;可不传,点击后无跳转',pushType:推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；sms:短信)}]", required = true, dataType = "List"),
            @ApiImplicitParam(name = "mtDriverSchoolList", value = "模板下发驾校数组[{dsId:0,dsCode:'驾校编号',dsName:'驾校名称'}]", required = true, dataType = "List"),
            @ApiImplicitParam(name = "optUser", value = "操作人", required = true, dataType = "Long", paramType = "Query")
    })
    @PostMapping("/update")
    public ResultEntity updateTemplate(@ApiParam(value = "入参") @RequestBody MessageTemplateVo params) throws Exception{
        messageTemplateService.updateTemplate(params);
        return ResultEntity.success("修改成功");
    }

    @ApiOperation(value = "删除消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "模板ID", required = true, dataType = "Long", paramType = "Query")
    })
    @PostMapping("/delete")
    public ResultEntity deleteTemplate(@RequestBody String ids) throws Exception{
        messageTemplateService.deleteTemplate(ids);
        return ResultEntity.success("删除成功");
    }

    @ApiOperation(value = "分页查询消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "mtCode", value = "模板编号", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "mtTitle", value = "模板标题", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "pushType", value = "推送方式(app:APP；wx_public:微信公众号；wx_applet:微信小程序；sms:短信)", dataType = "PushType", paramType = "Query"),
            @ApiImplicitParam(name = "fromTime", value = "开始时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "toTime", value = "截止时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "driverSchoolId", value = "驾校ID", dataType = "Long", paramType = "Query")
    })
    @PostMapping("/listPage")
    public PageBean<MessageTemplateItemDTO> listTemplate(@RequestBody MTConditionVo params) {
        return messageTemplateService.findByPage(params);
    }

    @ApiOperation(value = "查询所有消息模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "driverSchoolId", value = "驾校ID", dataType = "Long", paramType = "Query")
    })
    @PostMapping("/listAll")
    public List<MessageTemplateInfo> listAllTemplate(@RequestBody Long driverSchoolId) {
        return messageTemplateService.findAll(driverSchoolId);
    }

    @ApiOperation(value = "查看模板详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "模板ID", required = true, dataType = "Long", paramType = "Query")
    })
    @PostMapping("/detail")
    public MessageTemplateDTO detailNotice(@RequestBody Long dataId){
        return messageTemplateService.getTemplateDetail(dataId);
    }

}
